package com.wanbangee.marksystem.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wanbangee.marksystem.entities.UGrade;
import com.wanbangee.marksystem.entities.USchool;
import com.wanbangee.marksystem.service.UGradeService;

@RestController
@RequestMapping("gradeManage")
public class UGradeController {
	@Autowired
	private UGradeService uGradeService;

	/**
	 * 查询所有正常状态的schId 和 schNm
	 * 
	 * @return
	 */
	@GetMapping("selectSchool")
	public Map<String, Object> selectUSchool() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<USchool> schools = this.uGradeService.selectUSchool();
		if(schools !=null) {
			map.put("code", 1001);
			map.put("schools", schools);
			map.put("message", "接口成功");
			return map;
		}
		map.put("code", 1002);
		map.put("message", "接口失败");
		return map;
	}
	
	 /**
	    * 根据学校ID查询年级
	    * @param schId
	    * @return
	    */
	   @GetMapping("selectGradeBySchId")
	   public Map<String, Object>  selectGradeBySchId(Integer schId){
			Map<String, Object> map = new HashMap<String, Object>();
			List<UGrade> grades = this.uGradeService.selectGradeBySchId(schId);
			if(grades !=null) {
				map.put("code", 1001);
				map.put("grades", grades);
				map.put("message", "接口成功");
				return map;
			}
			map.put("code", 1002);
			map.put("message", "接口失败");
			return map;
		}
	  
	
	   /**
	    * 新增年级
	    * @param uGrade
	    * @return
	    */
	   @PostMapping("insertGrade")
	   public Map<String, Object>  insertGrade(UGrade uGrade){
			Map<String, Object> map = new HashMap<String, Object>();
			if(this.uGradeService.insertGrade(uGrade)==1) {
				map.put("code", 1001);
				map.put("message", "接口成功");
				return map;
			}
			map.put("code", 1002);
			map.put("message", "接口失败");
			return map;
		}
	   
	   /**
	    * 修改/删除年级前数据回显
	    * --根据年级ID查询年级信息
	    */
	   @GetMapping("selectGradeByGraId")
	   public Map<String, Object> selectGradeByGraId(Integer graId){
			Map<String, Object> map = new HashMap<String, Object>();
			UGrade uGrade=this.uGradeService.selectGradeByGraId(graId);
				map.put("code", 1001);
				map.put("message", "接口成功");
				map.put("uGrade", uGrade);
			return map;
		}
	  
	   
	   /**
	    * 修改年级
	    * @param uGrade
	    * @return
	    */
	   @PutMapping("updateGrade")
	   public Map<String, Object>  updateGrade(UGrade uGrade){
			Map<String, Object> map = new HashMap<String, Object>();
			if(	this.uGradeService.updateGrade(uGrade)==1) {
				map.put("code", 1001);
				map.put("message", "接口成功");
				return map;
			}
			map.put("code", 1002);
			map.put("message", "接口失败");
			return map;
		}
	   
	   /**
	    * 删除年级-将对应graId年级的状态改为0
	    * @param graId
	    * @return
	    */
	   @PutMapping("deleteGrade")
	   public Map<String, Object> deleteGrade(Integer graId){
			Map<String, Object> map = new HashMap<String, Object>();
			if(	this.uGradeService.deleteGrade(graId)==1) {
				map.put("code", 1001);
				map.put("message", "接口成功");
				return map;
			}
			map.put("code", 1002);
			map.put("message", "接口失败");
			return map;
		}
	   
	   /**
	    * 根据年级ID 修改年级状态为新状态
	    * @param graId
	    * @param graState
	    * @return
	    */
	   @PutMapping("updateGradeState")
	   public Map<String, Object> updateGradeStateBygraIdAndgraState(Integer graId,Integer graState){
			Map<String, Object> map = new HashMap<String, Object>();
			if(	this.uGradeService.updateGradeStateBygraIdAndgraState(graId, graState)==1) {
				map.put("code", 1001);
				map.put("message", "接口成功");
				return map;
			}
			map.put("code", 1002);
			map.put("message", "接口失败");
			return map;
		}
}
